package com.skytech.component.crawler.parse;

import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

@Service
public class ParseHtmlService {
    /**
     * 用来解析正文或者标题的方法
     * @param allElements 网页的所有内容
     * @param rules 用来解析目标的规则
     * @return 解析的结果
     */
    private Elements parse(Elements allElements,String rules ){
        allElements = allElements.select(rules);
        return allElements;
    }

    public String parseTitle(Elements allElements,String rules){
        Elements parse = parse(allElements, rules);
        if(parse.isEmpty()){
            return "";
        }
        return parse.get(0).html();
    }
    public String parseContent(Elements allElements,String rules){
        Elements parse = parse(allElements, rules);
        if(parse.isEmpty()){
            return "";
        }
        return parse.get(0).toString();
    }
}
